package com.ctgu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctgu.common.ResponseResult;
import com.ctgu.model.entity.Employee;
import com.ctgu.model.entity.JobWanted;
import com.ctgu.mapper.JobWantedMapper;
import com.ctgu.model.entity.LoginUser;
import com.ctgu.model.entity.Recruit;
import com.ctgu.model.vo.JobWantedVo;
import com.ctgu.service.IEmployeeService;
import com.ctgu.service.IExperienceService;
import com.ctgu.service.IJobWantedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@Service
public class JobWantedServiceImpl extends ServiceImpl<JobWantedMapper, JobWanted> implements IJobWantedService {

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public ResponseResult add(JobWanted jobWanted) {
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int id =loginUser.getUser().getId();
        jobWanted.setDate(LocalDateTime.now());
        jobWanted.setEId(loginUser.getUser().getId());
        LambdaQueryWrapper<JobWanted> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(JobWanted::getJobPosition,jobWanted.getJobPosition());
        queryWrapper.eq(JobWanted::getEId,id);
        JobWanted one = getOne(queryWrapper);
        ResponseResult employeeOne = employeeService.getEmployeeOne(id);
        Employee employee = (Employee) employeeOne.getData();
        if(employee.getWorkState().equals("在职")){
            return new ResponseResult<>(400,"您的工作状态为在职，无法发布求职");
        }
        if(Objects.nonNull(one)){
            return new ResponseResult<>(400,"该求职岗位已经存在请重新输入");
        }
        save(jobWanted);
        JobWantedVo jobWantedVo=new JobWantedVo();
        BeanUtils.copyProperties(jobWanted,jobWantedVo);
        String employeeName=employeeService.getById(loginUser.getUser().getId()).getName();
        jobWantedVo.setEmployeeName(employeeName);
        return new ResponseResult<>(200,"发布成功",jobWantedVo);

    }

    @Override
    public ResponseResult ListPage(Integer page, Integer pageSize, String jobPosition) {
        //分页构造器
        Page pageInfo=new Page(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<JobWanted> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(jobPosition),JobWanted::getJobPosition,jobPosition);
        Page Vo=new Page<>();
        BeanUtils.copyProperties(pageInfo,Vo,"records");
        List<JobWanted> list=list(wrapper);
        List<JobWantedVo> voList=list.stream().map(item->{
            JobWantedVo jobWantedVo=new JobWantedVo();
            BeanUtils.copyProperties(item,jobWantedVo);
            jobWantedVo.setEmployeeName(employeeService.getById(item.getEId()).getName());
            return jobWantedVo;
        }).collect(Collectors.toList());
        Vo.setRecords(voList);
        return new ResponseResult(200,"查询成功",Vo);
    }

    @Override
    public ResponseResult delete(Integer[] ids) {
        LambdaQueryWrapper<JobWanted> wrapper=new LambdaQueryWrapper();
        wrapper.in(JobWanted::getId,ids);
        remove(wrapper);
        return new ResponseResult<>(200,"删除成功");
    }

    @Override
    public ResponseResult getMyPage(int page, int pageSize) {
        LoginUser loginUser= (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String roleId = loginUser.getUser().getRoleId();
        if(!roleId.equals("3")){
            return new ResponseResult<>(400,"请登入员工账户");
        }
        Page pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<JobWanted> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(JobWanted::getEId,loginUser.getUser().getId());
        this.page(pageInfo,wrapper);
        Page JobWantedVo=new Page<>();
        BeanUtils.copyProperties(pageInfo,JobWantedVo,"records");
        List<JobWanted> list=list(wrapper);
        List<JobWantedVo> voList=list.stream().map(item->{
            JobWantedVo jobWantedVo=new JobWantedVo();
            BeanUtils.copyProperties(item,jobWantedVo);
            jobWantedVo.setEmployeeName(employeeService.getById(item.getEId()).getName());
            return jobWantedVo;
        }).collect(Collectors.toList());
        JobWantedVo.setRecords(voList);
        return new ResponseResult<>(200,"查询成功",JobWantedVo);
    }
}
